package gemini.castor.ui.client.page.content.store.shoppingcart;

import gemini.castor.ui.client.mvp.container.CastorPlaceContainer;
import gemini.castor.ui.client.mvp.container.CastorPlaceContainer.PresenterGetter;
import gemini.castor.ui.client.mvp.container.CastorWidgetContainerDisplay;
import gemini.castor.ui.client.mvp.container.CastorWidgetContainerPresenter;
import gemini.castor.ui.client.mvp.gin.CastorGinjector;
import gemini.castor.ui.client.page.content.store.shoppingcart.shoppingcartview.CartViewEvent;
import gemini.castor.ui.client.page.content.store.shoppingcart.shoppingcartview.CartViewHandler;
import gemini.castor.ui.client.page.content.store.shoppingcart.shoppingcartview.ShoppingCartViewObject;
import gemini.castor.ui.client.utils.PlaceConstants;

import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;
import ch.elca.gwt.mvp.client.place.PlaceRequest;

import com.google.inject.Inject;

public class ShoppingCartPresenter extends
		CastorWidgetContainerPresenter<ShoppingCartPresenter.Display> {

	public interface Display extends CastorWidgetContainerDisplay {
	}

	public static final Place PLACE = new CastorPlaceContainer<ShoppingCartPresenter>(
			PlaceConstants.PLACE_SHOPPING_CART, new PresenterGetter<ShoppingCartPresenter>() {
				public ShoppingCartPresenter getPresenter() {
					return CastorGinjector.INSTANCE.getShoppingCartPresenter();
				}
			});

	@Inject
	public ShoppingCartPresenter(Display display, EventBus eventBus) {
		super(display, eventBus);
		addPresenter(CastorGinjector.INSTANCE.getShoppingCartViewPresenter());
		addPresenter(CastorGinjector.INSTANCE.getCartCheckOutPresenter());
	}

	/*
	 * Register listeners on Display actions
	 */
	@Override
	protected void onBind() {
		super.onBind();
		
		registerHandlerRegister();

		showPresenter(CastorGinjector.INSTANCE
				.getShoppingCartViewPresenter(), createShoppingCartViewObject());
	}

	private ShoppingCartViewObject createShoppingCartViewObject() {
		ShoppingCartViewObject object = new ShoppingCartViewObject();
		
		List<ShoppingCartItem> items = CastorGinjector.INSTANCE.getRootPresenter().getItems();
		object.setItems(items);
		object.setDistributorCode(CastorGinjector.INSTANCE.getRootPresenter().getDistributorCode());
		object.setFullName(CastorGinjector.INSTANCE.getRootPresenter().getFullName());
		
		return object;
	}
	
	private void registerHandlerRegister() {
		registerHandler(getLocalEventBus().addHandler(CartViewEvent.getType(),
				new CartViewHandler() {
					
					@Override
					public void onCheckOut(CartViewEvent event) {
						Object paymentValue = getCurrentPresenter().getOutputObject();
						showPresenter(CastorGinjector.INSTANCE
								.getCartCheckOutPresenter(), paymentValue);
					}
				}));
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	protected void onPlaceRequest(PlaceRequest request) {
		// TODO Auto-generated method stub
	}
}
